<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-16 16:30:34
// +-----------------------------------------------------------------------


/**
 * [p 打印函数]
 * @param  string $value [传参]
 * @return [type]        [打印结果]
 */
function p($value = "") {
    echo "<pre>";
    print_r($value);
    echo "</pre>";
}

//检测验证码
function check_verify($code, $id = 1) {
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

//检测是否登陆
function is_login() {
    $user = session('user_auth');
    if (empty($user)) {
        return 0;
    } else {
        return $user['uid'];
    }
}

/**
 * [is_administrator 判断超级管理员]
 * @param  [type]  $id [description]
 * @return boolean     [返回真假]
 */
function is_administrator($uid = null) {
    $uid = is_null($uid) ? is_login() : $uid;
    return $uid && (in_array(intval($uid), C('USER_ADMINISTRATOR')));
}

//记录错误日志
function loginlog($uasername = null, $password = null, $loginerror = null) {
    $data['login_name'] = $uasername;
    $data['login_password'] = $password;
    $data['login_ip'] = ip2long(get_client_ip());
    $data['description'] = $loginerror;
    $data['login_time'] = time();
    $data['status'] = "1";
    M('Loginlog')->add($data);
}

//记录错误日志
function syslogs($type = null, $logname = null) {
    $data['type'] = $type;
    $data['logname'] = $logname;
    $data['username'] = session('user_auth') ['username'];
    $data['logip'] = ip2long(get_client_ip());
    $data['logtime'] = time();
    $data['status'] = "1";
    M('syslog')->add($data);
}

/**
 * 对查询结果集进行排序
 * @access public
 * @param array $list 查询结果
 * @param string $field 排序的字段名
 * @param array $sortby 排序类型
 * asc正向排序 desc逆向排序 nat自然排序
 * @return array
 */
function list_sort_by($list, $field, $sortby = 'asc') {
    if (is_array($list)) {
        $refer = $resultSet = array();
        foreach ($list as $i => $data) $refer[$i] = & $data[$field];
        switch ($sortby) {
            case 'asc':

                // 正向排序
                asort($refer);
                break;

            case 'desc':

                // 逆向排序
                arsort($refer);
                break;

            case 'nat':

                // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ($refer as $key => $val) $resultSet[] = & $list[$key];
        return $resultSet;
    }
    return false;
}

/**
 * 将list_to_tree的树还原成列表
 * @param  array $tree  原来的树
 * @param  string $child 孩子节点的键
 * @param  string $order 排序显示的键，一般是主键 升序排列
 * @param  array  $list  过渡用的中间数组，
 * @return array        返回排过序的列表数组
 */
function tree_to_list($tree, $child = '$child', $order = 'id', &$list = array()) {
    if (is_array($tree)) {
        foreach ($tree as $key => $value) {
            $reffer = $value;
            if (isset($reffer[$child])) {
                unset($reffer[$child]);
                tree_to_list($value[$child], $child, $order, $list);
            }
            $list[] = $reffer;
        }
        $list = list_sort_by($list, $order, $sortby = 'asc');
    }
    return $list;
}

/**
 * 把返回的数据集转换成Tree
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {

    // 创建Tree
    $tree = array();
    if (is_array($list)) {

        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = & $list[$key];
        }
        foreach ($list as $key => $data) {

            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] = & $list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = & $refer[$parentId];
                    $parent[$child][] = & $list[$key];
                }
            }
        }
    }
    return $tree;
}

//获取科室名称
function get_department_name($department_id = null) {
    if (!$department_id) {
        return;
    }
    $map['id'] = $department_id;
    $department_name = M('Department')->where($map)->getField('name');
    return $department_name;
}

//获取医院名称
function get_hospital_name($hospital_id = null) {
    if (!$hospital_id) {
        return;
    }
    if (is_array($hospital_id)) {
        $map['id'] = array('in', $hospital_id);
        $rs = M('Hospital')->where($map)->field('name')->select();
        foreach ($rs as $value) {
            $data[] = $value['name'];
        }
    } else {
        $map['id'] = $hospital_id;
        $data = M('Hospital')->where($map)->getField('name');
    }
    return $data;
}

//获取部门名称
function get_depart_name($depart_id = null) {
    if (!$depart_id) {
        return;
    }
    $map['id'] = $depart_id;
    $depart_name = M('Depart')->where($map)->getField('name');
    return $depart_name;
}

//获取科室name和科室所属医院id
function get_department_name_hp_id($department_id = null) {
    if (!$department_id) {
        return;
    }
    $map['id'] = $department_id;
    $department_name_hp_id = M('Department')->where($map)->field('name,hospital_id')->find();
    return $department_name_hp_id;
}

//获取用户名
function get_user_name() {
    $user = session('user_auth');
    return $user['realname'];
}
function get_depart_list() {
    $map['status'] = 1;
    $data = M('Depart')->where($map)->select();
    return $data;
}
function get_hospital_list() {
    $map['status'] = 1;
    $data = M('Hospital')->where($map)->select();
    return $data;
}

//定义时间
function today_begin() {
    $time = mktime(0, 0, 0);
    return $time;
}
function today_end() {
    $time = today_begin() + 24 * 3600;
    return $time;
}
function yesterday_begin() {
    $time = today_begin() - 24 * 3600;
    return $time;
}
function thismonth_begin() {
    $time = mktime(0, 0, 0, date("m"), 1);
    return $time;
}
function lastmonth_begin() {
    $time = strtotime("-1 month", thismonth_begin());
    return $time;
}
function days_in_month($month, $year) {

    // calculate number of days in a month
    return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
function text_show($string) {
    $string = str_replace(" ", "&nbsp;", $string);
    $string = str_replace(chr(13), "<br>", $string);
    $string = str_replace("\n", "<br>", $string);
    return $string;
}
?>
